package com.example.practice;


import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment4 extends Fragment {

    private View view;
    private GridView list4;

    public Fragment4() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment_fragment4, container, false);
        initView();
        initData();
        return view;
    }

    private void initData() {
        List<String> url=new ArrayList<>();
        List<String> urlname=new ArrayList<>();
        for (int i=0;i<4;i++){
            url.add("/sdcard/vv.mp4");
            urlname.add("视频名称"+i);
        }
        list4.setAdapter(new myGridView(url,urlname));
    }
    private void initView() {
        list4=view.findViewById(R.id.list_4);
    }
    private class myGridView extends BaseAdapter{
        List<String> url=new ArrayList<>();
        List<String> urlname=new ArrayList<>();

        public myGridView(List<String> url,List<String> urlname) {
            this.url=url;
            this.urlname=urlname;
        }

        @Override
        public int getCount() {
            return url.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }
        private class ViewHolder{
            ImageView imageView;
            TextView textView;
        }
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            final ViewHolder holder;
            if(view==null){
                view=LayoutInflater.from(getContext()).inflate(R.layout.vv_layout,null);
                holder=new ViewHolder();
                holder.imageView = view.findViewById(R.id.vv1);
                holder.textView = view.findViewById(R.id.vvt1);
                view.setTag(holder);
            }else {
                holder= (ViewHolder) view.getTag();
            }
            final Uri uri = Uri.parse(url.get(i));
            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                    LayoutInflater layoutInflater = LayoutInflater.from(getContext());
                    final View longinDialogView= layoutInflater.inflate(R.layout.layout, null);//找到定义的布局
                    builder.setView(longinDialogView);//设置布局
                    final VideoView videoView =  longinDialogView.findViewById(R.id.video);
                    builder.setCancelable(true);    //设置按钮是否可以按返回键取消,false则不可以取消
                    final AlertDialog dialog = builder.create();  //创建对话框
                    dialog.setCanceledOnTouchOutside(true); //设置弹出框失去焦点是否隐藏,即点击屏蔽其它地方是否隐藏
                    dialog.show();//展示Alert
                    videoView.setMediaController(new MediaController(getContext()));
                    videoView.setVideoURI(uri);
                    videoView.start();
                }
            });
            holder.textView.setText(urlname.get(i));
            return view;
        }
    }
}
